package com.hp.system.service.impl;

import com.hp.system.domain.*;
import com.hp.system.mapper.CarTypeMapper;
import com.hp.system.mapper.DestbationCityMapper;
import com.hp.system.mapper.IdentifyInfoMapper;
import com.hp.system.mapper.PassInfoMapper;
import com.hp.system.service.PassInfoService;
import com.hp.system.utils.JSONInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class PassInfoServiceImpl implements PassInfoService {
    @Autowired(required = false)
    private PassInfoMapper passInfoMapper;
    @Autowired(required = false)
    private IdentifyInfoMapper identifyInfoMapper;
    @Autowired(required = false)
    private CarTypeMapper carTypeMapper;
    @Autowired(required = false)
    private DestbationCityMapper destbationCityMapper;

    @Transactional
    @Override
    public int dealInfo(PassInfo passInfo){
        passInfo.setIsdeal(new Byte("2"));
        int addresult = passInfoMapper.insertSelective(passInfo);
        IdentifyInfo identifyInfo = identifyInfoMapper.selectByPrimaryKey(passInfo.getInid());
        identifyInfo.setIsdeal(new Byte("1"));
        identifyInfo.setDealTime(passInfo.getIndate());
        identifyInfo.setPassid(passInfoMapper.getOutPrimaryKey(passInfo));
        int updresult = identifyInfoMapper.updateByPrimaryKeySelective(identifyInfo);
        if(addresult == updresult && addresult == 1)
            return 1;
        else
            return -1;
    }
    @Transactional
    @Override
    public int carOutInfoDeal(IdentifyInfo identifyInfo) throws Exception {
        int addpass;
        int addiden=0;
        boolean issuccess = false;
        boolean isadd = true;
        PassInfo passInfo = new PassInfo();
        String license = identifyInfo.getLicense();
        //寻找是否有未处理的识别数据
        int number = identifyInfoMapper.selectCountByisdeal(2);
        if(number>0){
            System.out.println(number);
//            得到未处理的识别数据
            IdentifyInfoExample identifyInfoExample = new IdentifyInfoExample();
            IdentifyInfoExample.Criteria criteria = identifyInfoExample.createCriteria();
            criteria.andIsdealEqualTo(new Byte("2"));
            List<IdentifyInfo> list = identifyInfoMapper.selectByExample(identifyInfoExample);
            for (IdentifyInfo info: list
                    ) {
                //如果是重复的就不添加处理
                if(info.getSerialno().equals(identifyInfo.getSerialno()) && info.getLicense().equals(identifyInfo.getLicense()))
                    isadd = false;
            }
        }
        if (isadd)
            addiden = identifyInfoMapper.insertSelective(identifyInfo);
        if(addiden>0){
            IdentifyInfo info = identifyInfoMapper.getIdentifyInfo(identifyInfo);
            //根据车牌查找尚未出去的记录
            int count = passInfoMapper.selectCountByOutCar(license);
            //若无则新增一条出院记录，仅记录车牌时间与已处理状态
            if(count==0){
                passInfo.setLicense(license);
                passInfo.setOutid(info.getId());
                passInfo.setIsdeal(new Byte("1"));
                passInfo.setDealtime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(JSONInfoUtil.getTime()));
                passInfo.setOutdate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(JSONInfoUtil.getTime()));
                addpass = passInfoMapper.insertSelective(passInfo);
            }else {
                passInfo = passInfoMapper.selectOutCar(license);
                passInfo.setOutid(info.getId());
                passInfo.setOutdate(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(JSONInfoUtil.getTime()));
                addpass = passInfoMapper.updateByPrimaryKeySelective(passInfo);
            }
            if (addpass>0){
                info.setPassid(passInfoMapper.getPrimaryKey(passInfo));
                info.setIsdeal(new Byte("1"));
                info.setDealTime(passInfo.getOutdate());
                if(identifyInfoMapper.updateByPrimaryKeySelective(info)>0)
                    issuccess = true;
            }
        }
        if (issuccess)
            return 1;
        else
            return -1;
    }

    @Override
    public PassInfo showPassInfoById(Integer passid) {
        PassInfo passInfo = passInfoMapper.selectByPrimaryKey(passid);
        Integer belongid = passInfo.getCartype();
        Integer typeid = passInfo.getCarsize();
        Integer destid = passInfo.getDestbationid();
        Integer cityid = passInfo.getCityid();
        Integer purpose = passInfo.getPurpose();
        if(purpose!=null) {
            switch (purpose) {
                case 1:
                    passInfo.setPurposeInfo("卸货");
                    break;
                case 2:
                    passInfo.setPurposeInfo("装货");
                    break;
                case 3:
                    passInfo.setPurposeInfo("来访");
                    break;
                default:
                    break;
            }
        }
        if (destid != null){
            DestbationCity destidinfo = new DestbationCity();
            destidinfo.setDestbationid(destid);
            destidinfo.setCityid(cityid);
            DestbationCity destinfo = destbationCityMapper.selectByDest(destidinfo);
            String destname = destinfo.getDestbationvalue()+"-"+destinfo.getCityname();
            passInfo.setDestname(destname);
        }
        if(typeid !=null && belongid!=null){
            CarType carType = new CarType();
            carType.setBelongid(belongid);
            carType.setTypeid(typeid);
            CarType info = carTypeMapper.selectBybelongtype(carType);
            String cartypename = info.getBelongname()+"-"+info.getTypename();
            passInfo.setCartypename(cartypename);
        }
        return passInfo;
    }

    @Override
    public List<PassInfo> selectByPassInfo(PassInfo passInfo) {
        return passInfoMapper.selectByPassInfo(passInfo);
    }

    @Override
    public PassInfo getPassInfoByid(Integer passid) {
        return passInfoMapper.selectByPrimaryKey(passid);
    }

    @Override
    public PassInfo showPassInfoByIdentifyId(Integer id) {
        /**
         * 1.根据id查出识别信息，得到识别设备的序列号
         * 2.根据序列号判断进还是出
         * 3.判断进出后根据inid或者outid得到单条passid,调用showPassInfoById方法得到信息
         */
        PassInfo passInfo = null;
        IdentifyInfo identifyInfo = identifyInfoMapper.selectByPrimaryKey(id);
        if(identifyInfo.getSerialno().equals("08b6f3bc-b167693b"))
        {
            Integer passid = passInfoMapper.getPassidByOutid(id);
            passInfo = showPassInfoById(passid);
            System.out.println("城市编号为"+passInfo.getCityid());
            System.out.println("目的地名称为"+passInfo.getDestname());
        }else {
            Integer passid = passInfoMapper.getPassidByInid(id);
            passInfo = showPassInfoById(passid);
            System.out.println("城市编号为"+passInfo.getCityid());
            System.out.println("目的地名称为"+passInfo.getDestname());
        }
        return passInfo;
    }


    @Override
    public int updatePassInfo(PassInfo passInfo) {
        return passInfoMapper.updateByPrimaryKeySelective(passInfo);
    }

    @Override
    public List<PassInfo> getPassInfoSheet(PassInfo passInfo) {
        List<PassInfo> list = passInfoMapper.selectByPassInfo(passInfo);
        list.forEach(pinfo->{
            if(pinfo.getCartype()!=null && pinfo.getCarsize()!=null){
                Integer belongid = pinfo.getCartype();
                Integer typeid = pinfo.getCarsize();
                CarType carType = new CarType();
                carType.setBelongid(belongid);
                carType.setTypeid(typeid);
                CarType cinfo = carTypeMapper.selectBybelongtype(carType);
                String cartypename = cinfo.getBelongname()+"-"+cinfo.getTypename();
                pinfo.setCartypename(cartypename);
            }
            if (pinfo.getDestbationid()!=null) {
                Integer destid = pinfo.getDestbationid();
                DestbationCity destinfo = destbationCityMapper.selectByPrimaryKey(destid);
                String destname = destinfo.getDestbationvalue() + "-" + destinfo.getCityname();
                pinfo.setDestname(destname);
            }
        });
        return list;
    }

    @Override
    public String getCountByIsdeal() {
        return String.valueOf(passInfoMapper.getCountByIsdeal());
    }

}
